package site.yunnong.atvris.common.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author zjh
 * @date 2021/10/7 17:00
 */
@Configuration
@EnableAsync
public class ThreadPoolConfig {

    /**
     * 自定义线程池
     * @return
     */
    @Bean(name = "selfThreadPoolExecutor", destroyMethod = "shutdown")
    @ConditionalOnMissingBean(name = "selfThreadPoolExecutor") 
    public ThreadPoolExecutor selfThreadPoolExecutor() {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(9, 9, 5L, TimeUnit.MILLISECONDS,
                new LinkedBlockingDeque<>(13), new ThreadPoolExecutor.AbortPolicy());
        threadPoolExecutor.allowCoreThreadTimeOut(true);
        return threadPoolExecutor;
    }
    
}
